/*
 * Created by JFormDesigner on Fri Apr 15 23:33:02 CST 2022
 */

package com.ZengQi.event.view;

import com.ZengQi.event.controller.PublisherController;
import com.ZengQi.event.entity.Topic;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author QI Zeng
 */
public class AddTopicView extends JFrame {
    private PublisherController publisherController=new PublisherController();
    private String publisher;
    private Topic thisTopic =new Topic();
    public AddTopicView(String publisher) {
        this.publisher=publisher;
        initComponents();
        setVisible(true);
        setLocationRelativeTo(null);
    }

    //加题的事件监听
    private void addBtn(ActionEvent e) {
        if(NameTxt.getText()==null||"".equals(NameTxt.getText())||contentTxt.getText()==null||"".equals(contentTxt.getText())||answerArea.getText()==null||"".equals(answerArea.getText())){
            JOptionPane.showMessageDialog(null, "题目不能为空！");
            return;
        }
        thisTopic.setTopicName(NameTxt.getText());
        thisTopic.setTopicAnswer(answerArea.getText());
        thisTopic.setTopicContent(contentTxt.getText());
        thisTopic.setTopicPublisher(publisher);
        boolean b = publisherController.addTopic(thisTopic);
        if(b){
            JOptionPane.showMessageDialog(null, "添加成功！");
                dispose();
        }else{
            JOptionPane.showMessageDialog(null, "添加失败！");

        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        NameTxt = new JTextField();
        label2 = new JLabel();
        contentTxt = new JTextField();
        label3 = new JLabel();
        scrollPane1 = new JScrollPane();
        answerArea = new JTextArea();
        addBtn = new JButton();

        //======== this ========
        setIconImage(new ImageIcon(getClass().getResource("/bang.png")).getImage());
        setTitle("\u505a\u9898\u5e2e");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u9898\u76ee\u540d\u79f0");

        //---- label2 ----
        label2.setText("\u9898\u76ee\u5185\u5bb9");

        //---- label3 ----
        label3.setText("\u9898\u76ee\u7b54\u6848");

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(answerArea);
        }

        //---- addBtn ----
        addBtn.setText("\u786e\u8ba4\u6dfb\u52a0");
        addBtn.addActionListener(e -> addBtn(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(98, 98, 98)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(label2, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(contentTxt, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(NameTxt))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(label3, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(scrollPane1)))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 92, Short.MAX_VALUE)
                    .addComponent(addBtn)
                    .addGap(136, 136, 136))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(29, 29, 29)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1)
                        .addComponent(NameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(28, 28, 28)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2)
                        .addComponent(contentTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(47, 47, 47)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label3)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(39, 39, 39)
                            .addComponent(addBtn)))
                    .addContainerGap(78, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField NameTxt;
    private JLabel label2;
    private JTextField contentTxt;
    private JLabel label3;
    private JScrollPane scrollPane1;
    private JTextArea answerArea;
    private JButton addBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
